package com.irs.irsforum.web;


import com.irs.irsforum.entity.Comment;
import com.irs.irsforum.security.access.Access;
import com.irs.irsforum.security.access.UserType;
import com.irs.irsforum.service.IBehaviorService;
import com.irs.irsforum.service.ICommentService;
import com.irs.irsforum.service.IPostService;
import com.irs.irsforum.web.common.BaseController;
import com.irs.irsforum.web.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yihuili
 * @since 2020-01-18
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController {
    private static final Logger logger =
            LoggerFactory.getLogger(CommentController.class);


    @Autowired
    ICommentService iCommentService;
    @Autowired
    IBehaviorService iBehaviorService;
    @Autowired
    IPostService iPostService;

    /**
     * 添加评论，并增加行为
     *
     * @param comment
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/setcomment", method = RequestMethod.POST)
    public Object setComment(@RequestBody Comment comment,
                             @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId) {
        logger.info("添加评论comment={}", comment.toString());
        Integer commentResult = 0;
        try {
            comment.setUserId(userId);
            commentResult = iCommentService.setComment(comment);
            if (commentResult == 1) {
                iBehaviorService.setBehaviorType(comment.getPostId(), userId, 0);
                iPostService.updateCommentNum(comment.getPostId(), (long) 1);
                return ResponseResult.newSuccessResult("评论成功");
            } else {
                logger.info("userId={}的用户，评论post={}的帖子失败,commentResult ={}", userId, comment.getPostId(), commentResult);
                return ResponseResult.newFailResult("评论失败");
            }
        } catch (SQLException e) {
            logger.info("userId={}的用户，评论post={}的帖子失败,commentResult ={}", userId, comment.getPostId(), commentResult);
            return ResponseResult.newFailResult("评论失败");
        }
    }

    /**
     * 删除评论，并删除行为behaviorType
     *
     * @param postId    帖子Id
     * @param userId    用户Id
     * @param commentId 评论Id
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/delcomment", method = RequestMethod.DELETE)
    public Object delComment(@RequestParam(value = "postId") Long postId,
                             @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId,
                             @RequestParam(value = "commentId") Long commentId) {
        logger.info("删除评论postId={},userId={}", postId, userId);
        Integer result = iCommentService.delComment(commentId, userId);
        if (result == 1) {
            iBehaviorService.updateBehavior(postId, userId, 1);
            iPostService.updateCommentNum(postId, (long) -1);
            return ResponseResult.newSuccessResult("删除成功");
        } else {
            logger.error("删除评论postId={},userId={}失败", postId, userId);
            return ResponseResult.newFailResult("删除失败");
        }
    }

    /**
     * @param reply   回复内容，包括：post_id、comment_id、content
     * @param userId
     * @return
     */
    @Access(userType = {UserType.LOGIN_USER})
    @RequestMapping(value = "/reply2comment", method = RequestMethod.POST)
    public Object reply2Comment(@RequestBody Comment reply,
                                @ModelAttribute(OBJECT_USER_ATTRIBUTE) Long userId) {
        logger.info("回复评论commentId={},内容为={}", reply.getId(), reply.getContent());
        reply.setUserId(userId);
        Integer result = iCommentService.reply2comment(reply);
        if (result == 1) {
            iBehaviorService.setBehaviorType(reply.getPostId(), userId, 0);
            return ResponseResult.newSuccessResult("回复评论成功");
        } else {
            logger.error("回复评论commentId={},内容为={}失败", reply.getId(), reply.getContent());
            return ResponseResult.newFailResult("回复评论失败");
        }
    }

}

